package com.android.launcher3.widget;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Outline;
import android.hardware.display.VirtualDisplay;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.x.component.utils.BridgeSystemProperties;
import com.x.component.widget.WindowWidgetHost;

public class FirstScreenLayout1 extends FrameLayout {

    private final static double SCALE_LAND = 3.17;
    private final static double SCALE_PORTRAIT = 1;

    private VirtualDisplay virtualDisplay;

    public FirstScreenLayout1(@NonNull Context context) {
        super(context);
        init();
    }

    public FirstScreenLayout1(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FirstScreenLayout1(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.first_screen_layout1, this, true);
        setUpForLayout();

        screenAdaption();
    }

    public void notifyMapWidgetChanged(String pkgName, String className) {
        if (widgetSurfaceView != null && widgetSurfaceView.getVisibility() == VISIBLE) {
            WindowWidgetHost.getInstance().releaseWindowWidget("launcher-widget");
            widgetSurfaceView.setVisibility(View.GONE);
            widgetSurfaceView.setVisibility(View.VISIBLE);
        }
    }

    private SurfaceView widgetSurfaceView;
    private SurfaceHolder widgetSurfaceHolder;

    private void setUpForLayout() {
        widgetSurfaceView = findViewById(R.id.surface_widget);
        widgetSurfaceView.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                // 设置按钮圆角率为30
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), Utilities.pxFromDp(22, getContext().getResources().getDisplayMetrics()));

            }
        });
        widgetSurfaceView.setClipToOutline(true);
        widgetSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                widgetSurfaceHolder = surfaceHolder;
                String widgetContent = BridgeSystemProperties.getSystemProperty("persist.x.launcher.widget", "");
                String pkgName = "com.google.android.apps.maps";
                String className = "com.google.android.maps.MapsActivity";
                if (!TextUtils.isEmpty(widgetContent)) {
                    ComponentName widgetComponent = ComponentName.unflattenFromString(widgetContent);
                    assert widgetComponent != null;
                    Intent intent = getContext().getPackageManager().getLaunchIntentForPackage(widgetComponent.getPackageName());
                    if (intent != null && intent.resolveActivity(getContext().getPackageManager()) != null) {
                        pkgName = widgetComponent.getPackageName();
                        className = widgetComponent.getClassName();
                    }
                }
                Log.d("TinkerLauncher3", "layout3 surfaceCreated,pkgName: " + pkgName);
                Log.d("TinkerLauncher3", "layout3 surfaceCreated,className: " + className);
                ComponentName componentName = new ComponentName(pkgName, className);
                WindowWidgetHost.getInstance().init(getContext());
                WindowWidgetHost.getInstance().createWindowWidget("launcher-widget", widgetSurfaceView, componentName, widgetSurfaceView.getWidth(), widgetSurfaceView.getHeight(),
                        160, widgetSurfaceHolder.getSurface());
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                WindowWidgetHost.getInstance().releaseWindowWidget("launcher-widget");
            }
        });
    }


    private void screenAdaption() {

        if (Utilities.isLongStripScreen(getContext())) {
            return;
        }

        double designScale = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ? SCALE_PORTRAIT : SCALE_LAND;

        post(() -> {
            int width = getWidth();
            int height = getHeight();

            double scale = (width * 1.0) / (height * 1.0);

            if (scale > designScale) {
                width = (int) (height * designScale);
            } else {
                height = (int) ((width * 1.0) / designScale);
            }

            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
            layoutParams.width = width;
            layoutParams.height = height;
            setLayoutParams(layoutParams);
        });
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == VISIBLE) {
            screenAdaption();
        }
    }

}

